﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Weixin.MP.Map.Models
{
    public class WebCache
    {
        #region 缓存KEY
        /// <summary>
        /// 用户当前任务列表
        /// </summary>
        public const string WeatherXML = "WeatherXML";
        public const string WeatherCenterPoint = "WeatherCenterPoint";
        #endregion
        /// <summary>
        /// 清除指定的cachKey
        /// </summary>
        /// <param name="cacheKey"></param>
        public static void RemoveCache(string cacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Remove(cacheKey);
        }

        /// <summary>
        /// 获取当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        public static object GetCache(string cacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[cacheKey];
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值,过期时间为30秒
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="cacheValue"></param>
        public static void SetCache(string cacheKey, object cacheValue)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(cacheKey, cacheValue, null, DateTime.Now.AddSeconds(30),
                            System.Web.Caching.Cache.NoSlidingExpiration);
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="cacheValue"></param>
        /// <param name="expireTime">过期时间(秒)</param>
        public static void SetCache(string cacheKey, object cacheValue, int expireTime)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(cacheKey, cacheValue, null, DateTime.Now.AddSeconds(expireTime),
                            System.Web.Caching.Cache.NoSlidingExpiration);
        }

        private static object lockHelp = new object();

        public static T Get<T>(string key, Func<object[], T> getCacheItemFunc, int expireTime, params object[] args)
        {
            if ((GetCache(key) == null))
            {
                lock (lockHelp)
                {
                    if ((GetCache(key) == null))
                    {
                        SetCache(key, getCacheItemFunc(args), expireTime);
                    }
                }
            }
            return (T)GetCache(key);
        }

        public static void Update<T>(string key, T value, int expireTime)
        {
            if (GetCache(key) != null)
            {
                lock (lockHelp)
                {
                    if (GetCache(key) != null)
                    {
                        RemoveCache(key);
                        SetCache(key, value, expireTime);
                    }
                }
            }

        }
    }
}